﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace zipTree
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string unZipFile =
                HttpContext.Current.Server.MapPath(@"UnZipFile") + "\\LINQPad4.zip";
            this.relateTreeView(this.TreeViewDemo, unZipFile);
        }

        //使用relateTreeView函数时 path 路径使用本地路径,或经过Server.MapPath("服务器的文件路径")解析过的路径
        //例如：relateTreeView(TreeView1, "D:\\Download");
        public void relateTreeView(TreeView tv, string path) {
            tv.Nodes.Clear();                          //清空TreeView
            tv.Nodes.Add(new TreeNode());              //添加新节点
            string[] pathinfo = Path.GetFullPath(path.Trim()).Split(char.Parse("\\")); //得到文件路径数组
            tv.Nodes[0].Text = pathinfo[pathinfo.Length - 1]; //得到文件夹名
            tv.Nodes[0].Value = path;                  //得到文件夹的详细本地路径
            tv.Nodes[0].Expanded = true;               //展开根节点 
            //tv.Nodes[0].ImageUrl = "../img/2.bmp"; //根节点图片
            TraversingCatalog(tv.Nodes[0], path);      //调用函数
        }
        public bool TraversingCatalog(TreeNode tn, string path) //遍历文件夹
        {
            if (Directory.Exists(path) == false) { return false; }
            DirectoryInfo dirInfo = new DirectoryInfo(path);

            int allNum = dirInfo.GetDirectories().Length + dirInfo.GetFiles("*.*").Length;
            if (allNum == 0) //没有任何文件夹和文件就建立"（空白）"节点并返回false
            {
                TreeNode empty = new TreeNode();
                empty.Text = "（空白）";  //得到文件名
                empty.Value = "";         //得到文件的详细本地路径
                //empty.ImageUrl = "";      //节点图片
                //empty.Expanded = false;   //折叠节点
                tn.ChildNodes.Add(empty); //添加新节点
                return false;
            }

            //循环文件夹(避免混乱,先循环文件夹)
            int folderIndex = -1; //文件夹索引
            foreach (DirectoryInfo folder in dirInfo.GetDirectories()) {
                folderIndex++;
                TreeNode folderNode = new TreeNode();
                folderNode.Text = folder.Name;      //得到文件夹名
                folderNode.Value = folder.FullName; //得到文件夹的详细本地路径
                folderNode.ToolTip = folder.Name;   //得到文件夹名
                folderNode.Expanded = false;        //折叠节点
                //folderNode.ImageUrl = "../img/2.bmp"; //节点图片
                tn.ChildNodes.Add(folderNode);      //添加新节点
                TraversingCatalog(tn.ChildNodes[folderIndex], path + "/" + folder.Name); //递归遍历其它文件夹
            }

            //循环文件
            foreach (FileInfo file in dirInfo.GetFiles("*.*")) //循环扩展名为*.*的文件
            {
                TreeNode fileNode = new TreeNode();
                fileNode.Text = file.Name;      //得到文件名
                fileNode.Value = file.FullName; //得到文件的详细本地路径
                fileNode.ToolTip = file.Name;   //得到文件名
                fileNode.Expanded = false;      //折叠节点
                //fileNode.ImageUrl = "../img/2.bmp"; //节点图片
                tn.ChildNodes.Add(fileNode);    //添加新节点
            }
            return true;
        }

        protected void TreeViewDemo_SelectedNodeChanged(object sender, EventArgs e) {
            string name = this.TreeViewDemo.SelectedNode.Value;
            FileInfo file = new FileInfo(name);
            StreamReader reader = new StreamReader(name, System.Text.ASCIIEncoding.UTF8);
            string text = reader.ReadToEnd();

            System.Collections.Specialized.NameValueCollection nc = new System.Collections.Specialized.NameValueCollection(Request.Form);
            nc.GetValues("content")[0] = text;
        }
    }
}
